/* FINCoS Framework
 * Copyright (C) 2013 CISUC, University of Coimbra
 *
 * Licensed under the terms of The GNU General Public License, Version 2.
 * A copy of the License has been included with this distribution in the
 * fincos-license.txt file.
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2 of the License, or (at your option)
 * any later version. This program is distributed in the hope that it will be
 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 *
 * See the GNU General Public License for more details.
 */


package pt.uc.dei.fincos.controller.gui;

import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.UIManager;

import pt.uc.dei.fincos.basic.RandomDomain;
import pt.uc.dei.fincos.random.RandomExponentialVariate;
import pt.uc.dei.fincos.random.RandomNormalVariate;
import pt.uc.dei.fincos.random.RandomUniformVariate;
import pt.uc.dei.fincos.random.Variate;

/**
 *
 * GUI for comfiguring sequential domains.
 *
 * @author  Marcelo R.N. Mendes
 *
 */
@SuppressWarnings("rawtypes")
public final class RandomlDomainPanel extends javax.swing.JPanel {

    /** serial id. */
    private static final long serialVersionUID = -2474137429601814058L;

    /** Creates new form RandomlDomainPanel. */
    public RandomlDomainPanel() {
        initComponents();
        addListeners();
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        distrCombo = new javax.swing.JComboBox();
        param1Label = new javax.swing.JLabel();
        param1Field = new javax.swing.JTextField();
        param2Label = new javax.swing.JLabel();
        param2Field = new javax.swing.JTextField();

        setMaximumSize(new java.awt.Dimension(266, 211));
        setMinimumSize(new java.awt.Dimension(266, 211));
        setPreferredSize(new java.awt.Dimension(266, 211));

        jLabel1.setText("Distribution");

        distrCombo.setFont(new java.awt.Font("Tahoma", 0, 10));
        distrCombo.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Uniform", "Normal", "Exponential" }));

        param1Label.setFont(new java.awt.Font("Tahoma", 0, 10));
        param1Label.setText("lower");

        param1Field.setFont(new java.awt.Font("Tahoma", 0, 10));

        param2Label.setFont(new java.awt.Font("Tahoma", 0, 10));
        param2Label.setText("upper");

        param2Field.setFont(new java.awt.Font("Tahoma", 0, 10));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addContainerGap()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addGap(10, 10, 10)
                                .addComponent(distrCombo, 0, 144, Short.MAX_VALUE))
                            .addComponent(jLabel1)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(38, 38, 38)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(param1Label, javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(param2Label, javax.swing.GroupLayout.Alignment.TRAILING))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(param2Field)
                            .addComponent(param1Field, javax.swing.GroupLayout.DEFAULT_SIZE, 72, Short.MAX_VALUE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 23, Short.MAX_VALUE)))
                .addContainerGap(102, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(distrCombo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(param1Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(param1Label))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(param2Label)
                    .addComponent(param2Field, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(90, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void addListeners() {
        distrCombo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (distrCombo.getSelectedItem().equals("Uniform")) {
                    param1Label.setText("lower");
                    param2Label.setText("upper");
                    param2Label.setVisible(true);
                    param2Field.setVisible(true);
                } else if (distrCombo.getSelectedItem().equals("Normal")) {
                    param1Label.setText("mean");
                    param2Label.setText("stdev");
                    param2Label.setVisible(true);
                    param2Field.setVisible(true);
                } else if (distrCombo.getSelectedItem().equals("Exponential")) {
                    param1Label.setText("lambda");
                    param2Label.setVisible(false);
                    param2Field.setVisible(false);
                }
            }
        });
    }

    protected void fillProperties(RandomDomain domain) {
        Variate v = domain.getVariate();

        if (v instanceof RandomExponentialVariate) {
            distrCombo.setSelectedItem("Exponential");
            param1Field.setText(((RandomExponentialVariate) v).getLambda() + "");
        } else if (v instanceof RandomNormalVariate) {
            distrCombo.setSelectedItem("Normal");
            param1Field.setText(((RandomNormalVariate) v).getMean() + "");
            param2Field.setText(((RandomNormalVariate) v).getStdev() + "");
        } else if (v instanceof RandomUniformVariate) {
            distrCombo.setSelectedItem("Uniform");
            param1Field.setText(((RandomUniformVariate) v).getLower() + "");
            param2Field.setText(((RandomUniformVariate) v).getUpper() + "");
        }
    }

    /**
     *
     * Checks if the fields in the UI have been correctly filled.
     *
     * @return  <tt>true</tt> if all the parameters have been correctly filled,
     *          <tt>false</tt> otherwise
     */
    public boolean validateFields() {
        boolean ret = true;

        if (param1Field.getText() == null
         || param1Field.getText().isEmpty()) {
            param1Field.setBackground(ComponentDetail.INVALID_INPUT_COLOR);
            ret = false;
        } else {
            try {
                String p1 = param1Field.getText();
                Double.parseDouble(p1);
                Color defaultColor = UIManager.getColor("TextField.background");
                param1Field.setBackground(defaultColor);
            } catch (NumberFormatException nfe) {
                param1Field.setBackground(ComponentDetail.INVALID_INPUT_COLOR);
                ret = false;
            }
        }

        if (!distrCombo.getSelectedItem().equals("Exponential")) {
            if (param2Field.getText() == null
                    || param2Field.getText().isEmpty()) {
                param2Field.setBackground(ComponentDetail.INVALID_INPUT_COLOR);
                ret = false;
            } else {
                try {
                    String p2 = param2Field.getText();
                    Double.parseDouble(p2);
                    Color defaultColor = UIManager.getColor("TextField.background");
                    param2Field.setBackground(defaultColor);
                } catch (NumberFormatException nfe) {
                    param2Field.setBackground(ComponentDetail.INVALID_INPUT_COLOR);
                    ret = false;
                }
            }
        }


        return ret;
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    protected javax.swing.JComboBox distrCombo;
    private javax.swing.JLabel jLabel1;
    protected javax.swing.JTextField param1Field;
    protected javax.swing.JLabel param1Label;
    protected javax.swing.JTextField param2Field;
    protected javax.swing.JLabel param2Label;
    // End of variables declaration//GEN-END:variables
}
